<?php

namespace App\Models\OpenShop\InvestmentPromotion;

use App\Enums\OpenShop\AreaBaseEnum;
use App\Enums\OpenShop\InvestmentPromotion\UserBaseInfoEnum;
use App\Models\BaseModel;
use App\Models\Traits\KfUid;
use App\Service\OpenShop\Feishu\ConfigService;
use App\Support\ChangeSupport;

class OpenShopUserBaseInfoModel extends BaseModel
{
    use KfUid;

    const SOURCE_WECHAT  = 0;
    const SOURCE_BAIDU   = 1;
    const SOURCE_ONESELF = 2;

    const SOURCE_TYPE = [
        self::SOURCE_WECHAT  => '微信公众号',
        self::SOURCE_BAIDU   => '百度竞价',
        self::SOURCE_ONESELF => '自主招商',
    ];

    protected $table   = 'open_shop_user_base_info';
    protected $appends = [
        'area',
        'province',
        'city',
        'district',
        'leasingManager',
        'sourceText',
    ];

    public static function getOrderLogTranslate(): array
    {
        return [
            "mobile"            => "手机号",
            "remarkMobile1"     => "备注手机号 - 1",
            "remarkMobile2"     => "备注手机号 - 2",
            "weChatNumber"      => "微信号",
            "isMarried"         => [
                "type" => ChangeSupport::FILED_TYPE_ENUM,
                "enum" => collect(ConfigService::config("openShop")["isMarried"])
                    ->pluck("name", "id")->toArray(),
                "desc" => "是否已婚"
            ],
            "addressPrefix"     => "现住地址",
            "address"           => "详细地址信息",
            "postCode"          => "邮编",
            "source"            => [
                "type" => ChangeSupport::FILED_TYPE_ENUM,
                "enum" => collect(ConfigService::config("openShop")["source"])
                    ->pluck("name", "id")->toArray(),
                "desc" => "来源"
            ],
            "isSelfManaged"     => "是否亲自经营",
            "joinReason"        => [
                "type" => ChangeSupport::FILED_TYPE_ENUM,
                "enum" => collect(ConfigService::config("openShop")["joinReason"])
                    ->pluck("name", "id")->toArray(),
                "desc" => "加盟原因"
            ],
            "shopBudget"        => "开店预算",
            "investmentMode"    => [
                "type" => ChangeSupport::FILED_TYPE_ENUM,
                "enum" => collect(ConfigService::config("openShop")["investmentMode"])
                    ->pluck("name", "id")->toArray(),
                "desc" => "加盟方式"
            ],
            "financeSource"     => [
                "type" => ChangeSupport::FILED_TYPE_ENUM,
                "enum" => collect(ConfigService::config("openShop")["financeSource"])
                    ->pluck("name", "id")->toArray(),
                "desc" => "资金来源"
            ],
            "shopAddressPrefix" => "门店地址前缀 【省市区】",
            "isSelfWithAddress" => [
                "type" => ChangeSupport::FILED_TYPE_ENUM,
                "enum" => UserBaseInfoEnum::IS_SELF_WITH_ADDRESS,
                "desc" => "是否自带选址"
            ],

            "chooseShopAddressPrefix" => "门店选址区域前缀 【省市区】",
            "shopName"                => "门店名称",
            "shopAddress"             => "门店地址",

            "chooseAddressReason" => [
                "type" => ChangeSupport::FILED_TYPE_ENUM,
                "enum" => collect(ConfigService::config("openShop")["chooseAddressReason"])
                    ->pluck("name", "id")->toArray(),
                "desc" => "选址原因"
            ],
            "email"               => "邮箱",
            "cardAccountName"     => "账户名称",
            "cardBankName"        => "开户行",
            "cardNo"              => "银行账号",
        ];
    }

    public function getAreaAttribute()
    {
        return AreaBaseEnum::provinceToRegion(explode('-', $this->addressPrefix)[0] ?? '未知地区');
    }

    public function getProvinceAttribute()
    {
        return explode('-', $this->addressPrefix)[0] ?? '未知地区';
    }

    public function getCityAttribute()
    {
        return explode('-', $this->addressPrefix)[1] ?? '未知地区';
    }

    public function getDistrictAttribute()
    {
        return explode('-', $this->addressPrefix)[2] ?? '未知地区';
    }

    //根据创建人ID获取姓名
    public function getLeasingManagerAttribute()
    {
        return $this->createdByUid;
    }

    //模型关联
    public function userContractRel()
    {
        return $this->hasOne(OpenShopUserContractModel::class, 'orderNo', 'orderNo');
    }

    public function getSourceTextAttribute()
    {
        return ConfigService::getConfigText("openShop", "source", $this->source);
    }

    protected $fillable = [
        'orderNo',
        'mobile',
        'remarkMobile1',
        'remarkMobile2',
        'weChatNumber',
        'isMarried',
        'provinceCode',
        'cityCode',
        'districtCode',
        'addressPrefix',
        'address',
        'postCode',
        'source',
        'isSelfManaged',
        'joinReason',
        'shopBudget',
        'investmentMode',
        'financeSource',
        'shopProvinceCode',
        'shopCityCode',
        'shopDistrictCode',
        'shopAddressPrefix',
        'isSelfWithAddress',
        'chooseAddressReason',
        'createdByUid',
        'updatedByUid',
        'createdAt',
        'updatedAt',
        'chooseShopProvinceCode',
        'chooseShopCityCode',
        'chooseShopDistrictCode',
        'shopName',
        'shopAddress',
        'chooseAddressReason',
        'cardBankName',
        'cardAccountName',
        'cardNo',
        'clueNo',
        'email',
        'chooseShopAddressPrefix'
    ];

    public static function insert($data)
    {
        return self::query()->insert($data);
    }

    /**
     * 根据字段查询用户单条数据
     * @param array $column ['id'=>11,'name'=>'222']
     * @param array $field ['id','name','age']
     */
    public static function getUserInfoOneByColumn(array $column, array $field)
    {
        return self::query()
                   ->when(!empty($data), function ($query) use ($field) {
                       $query->select($field);
                   })
                   ->when(!empty($column), function ($query) use ($column) {
                       $query->where($column);
                   })
                   ->first();
    }

    /**
     * 查询用户基本信息列表
     * @param array $column
     * @param array $field
     */
    public static function getUserListByColumn(array $column = [], array $field = [])
    {
        return self::query()
                   ->when(!empty($field), function ($query) use ($field) {
                       $query->select($field);
                   })
                   ->when(!empty($column), function ($query) use ($column) {
                       $query->where($column);
                   })
                   ->get();
    }

    public static function getDataByOrderNo(string $orderNo)
    {
        return self::query()->where('orderNo', $orderNo)->first();
    }
}
